package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.mapper.DepositChannelsMapper;
import com.mapper.OperateRecordsMapper;
import com.mapper.DepositOrdersMapper;
import com.model.*;
import com.util.ConstantUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@PropertySource("classpath:message.properties")
public class DepositController {

	
	@Autowired
	private Environment env;


    @Autowired
    private DepositOrdersMapper depositOrdersMapper;


    @Autowired
    private DepositChannelsMapper depositChannelsMapper;


    @Value("${welcome.message:test}")
	private String message = "Hello World";



	@RequestMapping("/deposit")
    public String depositList(Map<String, Object> model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(ConstantUtil.loginSession);
        List<DepositChannels> depositeTypeList = depositChannelsMapper.getDepositeChannelsByPermissionId(user.getDepositGroupId());
        model.put("pagename","deposit");
        model.put("list",depositeTypeList);
        return "usercenter";
    }


    @RequestMapping("/addDepositOrder")
    @ResponseBody
    public ResponseMsg addDepositOrder(HttpServletRequest request) {
        ResponseMsg msg = new ResponseMsg();
        String depositMoney = request.getParameter("depositMoney");
        String depositChannelId = request.getParameter("depositChannelId");
        String bankCode = request.getParameter("bankCode");

        if(!StringUtils.isNumeric(depositChannelId)){
            msg.setStatus( ResponseMsg.failed );
            msg.setData("系统异常，请联系客服!");
            return msg;
        }

        if(StringUtils.isBlank(depositMoney)){
            msg.setStatus( ResponseMsg.failed );
            msg.setData("存款金额不能为空!");
            return msg;
        }

        double requestAmount = Double.parseDouble(depositMoney);
        DepositChannels channel = depositChannelsMapper.getDepositeChannelsById(Integer.parseInt(depositChannelId));
        if(requestAmount < channel.getMinDepositAmount()){
            msg.setStatus( ResponseMsg.failed );
            msg.setData("存款金额不能少于"+channel.getMinDepositAmount());
            return msg;
        }

        if(requestAmount > channel.getMaxDepositAmount()){
            msg.setStatus( ResponseMsg.failed );
            msg.setData("存款金额不能大于"+channel.getMaxDepositAmount());
            return msg;
        }


        DepositOrder order = new DepositOrder();
        order.setRequestDepositAmount(Double.parseDouble(depositMoney));
        order.setBankCode(bankCode);

      /*  if(!StringUtils.isEmpty(offlineFlag)){
            order.setOfflineaccountname(channel.getOfflineaccountname());
            order.setOfflinearea(channel.getOfflinearea());
            order.setOfflinebankname(channel.getOfflinebankname());
            order.setOfflineprovince(channel.getOfflineprovince());
        }*/
        depositOrdersMapper.addDepositOrder(order);
        msg.setStatus( ResponseMsg.success );
        msg.setData(channel.getRedircturl());
        return msg;
    }




    public Environment getEnv() {
        return env;
    }

    public void setEnv(Environment env) {
        this.env = env;
    }

    public DepositOrdersMapper getDepositOrdersMapper() {
        return depositOrdersMapper;
    }

    public void setDepositOrdersMapper(DepositOrdersMapper depositOrdersMapper) {
        this.depositOrdersMapper = depositOrdersMapper;
    }

    public DepositChannelsMapper getDepositChannelsMapper() {
        return depositChannelsMapper;
    }

    public void setDepositChannelsMapper(DepositChannelsMapper depositChannelsMapper) {
        this.depositChannelsMapper = depositChannelsMapper;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
